package com.xinjie.www.dao.comment;

import com.xinjie.www.dao.BaseDao;
import com.xinjie.www.po.Comment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl implements CommentDao {
    //获取评论
    @Override
    public List<Comment> getComment(Connection connection, int post) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Comment> comments = new ArrayList<>();
        Object[] params = {post};
        String sql = "select * from comment where post_id = ?";
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Comment comment = new Comment();
            comment.setCommentId(rs.getInt("comment_id"));
            comment.setPostId(rs.getInt("post_id"));
            comment.setUserId(rs.getInt("user_id"));
            comment.setContent(rs.getString("content"));
            comment.setTime(rs.getString("time"));
            comments.add(comment);
        }
        BaseDao.closeResource(null, pstm, rs);
        return comments;
    }

    //获取管理员管理的评论
    @Override
    public List<Comment> getManagerComment(Connection connection, String tieba) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Comment> comments = new ArrayList<>();
        Object[] params = {tieba};
        String sql = "select c.* from comment c,post p where c.post_id=p.post_id and p.tieba = ?";
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Comment comment = new Comment();
            comment.setCommentId(rs.getInt("comment_id"));
            comment.setPostId(rs.getInt("post_id"));
            comment.setUserId(rs.getInt("user_id"));
            comment.setContent(rs.getString("content"));
            comment.setTime(rs.getString("time"));
            comments.add(comment);
        }
        BaseDao.closeResource(null, pstm, rs);
        return comments;
    }

    //写评论
    @Override
    public int writeComment(Connection connection, int postId, int userId, String content, String time) throws SQLException {
        PreparedStatement pstm = null;
        Object[] params1 = {postId, userId, content, time};
        String sql1 = "insert into comment (post_id,user_id,content,time) values(?,?,?,?)";
        int excute = BaseDao.excute(connection, pstm, sql1, params1);
        if (excute > 0) {
            String sql2 = "update post set comment = comment+1 where post_id = ?";
            Object[] params2 = {postId};
            BaseDao.excute(connection, pstm, sql2, params2);
        }
        BaseDao.closeResource(null, pstm, null);
        return excute;
    }

    //获取某用户的评论
    @Override
    public List<Comment> getOwnComment(Connection connection, int userId) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Comment> comments = new ArrayList<>();
        String sql = "select * from comment where user_id = ?";
        Object[] params = {userId};
        rs = BaseDao.excute(connection, pstm, rs, sql, params);
        while (rs.next()) {
            Comment comment = new Comment();
            comment.setCommentId(rs.getInt("comment_id"));
            comment.setPostId(rs.getInt("post_id"));
            comment.setUserId(rs.getInt("user_id"));
            comment.setContent(rs.getString("content"));
            comment.setTime(rs.getString("time"));
            comments.add(comment);
        }
        BaseDao.closeResource(null, pstm, rs);
        return comments;
    }

    //用户删除自己的评论
    @Override
    public int deleteOwnComment(Connection connection, int commentId, int postId) throws SQLException {
        PreparedStatement pstm = null;
        Object[] params1 = {commentId};
        String sql1 = "delete from comment where comment_id = ?";
        int excute = BaseDao.excute(connection, pstm, sql1, params1);
        if (excute > 0) {
            Object[] params2 = {postId};
            String sql2 = "update post set comment = comment-1 where post_id=?";
            BaseDao.excute(connection, pstm, sql2, params2);
        }
        BaseDao.closeResource(null, pstm, null);
        return excute;
    }
}
